<?php
ob_start();
ob_implicit_flush(0);
session_start();
/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */
/*
$start = microtime ();
$start = explode ( " ", $start );
$start = $start [1] + $start [0];
*/

//error_reporting(E_ALL|E_STRICT);
error_reporting(0);
//error_reporting(E_ALL|E_STRICT);
defined('BASE_PATH') || define('BASE_PATH', realpath(dirname(__FILE__)));
include_once ("config/config.php");
include_once ("config/common.php");
ob_start("replaceTitle");
if (isset($_GET ['path'])) {
    $page = filter ($_GET ['path']);
}else {
    $page = 'home';
}
$params = explode('/', $page);
//$params[0] is difine for page
//$params[1] is difine for module
if($params[0]== $config ['admin_section'] || $params[0]=='favicon.ico') {//admin

// remember url
    if(isset($_GET ['path']) AND !strpos($_GET['path'], 'login') AND !strpos($_GET['path'], 'logout')) {
        $_SESSION['path'] = $page;
    }
    if(count($params)>=2 AND $params[1]!='' AND $params[1]!='favicon.ico') {
        $adminModule = $params[1];
        if(@$params[2]!='' && intval($params[2])>0) {
            $_SESSION['articlePageEdit'] = $config ['page']=$params[2];
        }else {
            $config ['page']=1;
        }
    }else {
        $adminModule = 'article';
    }
    $_SESSION['adminActiveMenu']=$adminModule;
    if (file_exists($config ['admin_function_path']."/$adminModule/$adminModule-functions.php")) {
        include_once $config ['admin_function_path']."/$adminModule/$adminModule-functions.php";
    }
    if (file_exists($config ['admin_document_root']."/modules/$adminModule.php")) {
        include_once $config ['admin_document_root']."/modules/$adminModule.php";
    }else {
        header("Location: ".$config['domain']);
    }
}else {//client
    $_SESSION['clientActiveMenu']=$params[0];

    if(strpos($params[0], '.html')) {//detai page
        $_SESSION['clientActiveMenu']='detail';
        $toGetArticleLink = explode('.', $params[0]);
        $config ['link']=$toGetArticleLink[0];//article link
        if (file_exists($config ['client_function_path']."/detail/detail-functions.php")) {
            include_once $config ['client_function_path']."/detail/detail-functions.php";
        }
        if (file_exists($config ['client_document_root']."/modules/detail.php")) {
            include_once $config ['client_document_root']."/modules/detail.php";
        }else {
            header("Location: ".$config['domain']);
        }
    }else if($params[0]=='cata') {
            $_SESSION['pathWay']=$config ['link']=$params[1];
            if(intval($params[3])>0) {
                $config ['page']=$params[3];
            }
            if (file_exists($config ['client_function_path']."/category/category-functions.php")) {
                include_once $config ['client_function_path']."/category/category-functions.php";
            }
            if (file_exists($config ['client_document_root']."/modules/category.php")) {
                include_once $config ['client_document_root']."/modules/category.php";
            }else {
                header("Location: ".$config['domain']);
            }
        }else if($params[0]=='topread') {
                $_SESSION['pathWay']=$params[0];
                if(intval($params[2])>0) {
                    $config ['page']=$params[2];
                }
                if (file_exists($config ['client_function_path']."/topread/topread-functions.php")) {
                    include_once $config ['client_function_path']."/topread/topread-functions.php";
                }
                if (file_exists($config ['client_document_root']."/modules/topread.php")) {
                    include_once $config ['client_document_root']."/modules/topread.php";
                }else {
                    header("Location: ".$config['domain']);
                }
            }else if($params[0]=='rss') {
                    $config ['link']=$params[1];
                    if(intval($params[3])>0) {
                        $config ['page']=$params[3];
                    }
                    if (file_exists($config ['client_function_path']."/rss/rss-functions.php")) {
                        include_once $config ['client_function_path']."/rss/rss-functions.php";
                    }
                    if (file_exists($config ['client_document_root']."/modules/rss.php")) {
                        if($config ['link']=='') {
                            include_once $config ['client_document_root']."/modules/rssListUrl.php";
                        }else {
                            include_once $config ['client_document_root']."/modules/rss.php";
                        }
                    }else {
                        header("Location: ".$config['domain']);
                    }
                }else if($params[0]=='search') {
                        if($params[1]=='' || $params[1]==' ' || $params[1]=='"' || $params[1]=='\'' || $params[1]=='*') {
                            header("Location: ".$config['domain']);
                        }else {
                            $config['keyword'] = $params[1];
                        }
                        if(intval($params[3])>0) {
                            $config ['page']=$params[3];
                        }else {
                            $config ['page']=1;
                        }
                        if (file_exists($config ['client_function_path']."/search/search-functions.php")) {
                            include_once $config ['client_function_path']."/search/search-functions.php";
                        }
                        if (file_exists($config ['client_document_root']."/modules/search.php")) {
                            include_once $config ['client_document_root']."/modules/search.php";
                        }else {
                            header("Location: ".$config['domain']);
                        }
                    }else if($params[0]=='tag') {
                            $_SESSION['pathWay']=$config ['link']=$params[1];
                            if(intval($params[3])>0) {
                                $config ['page']=$params[3];
                            }
                            if (file_exists($config ['client_function_path']."/tag/tag-functions.php")) {
                                include_once $config ['client_function_path']."/tag/tag-functions.php";
                            }
                            if (file_exists($config ['client_document_root']."/modules/tag.php")) {
                                include_once $config ['client_document_root']."/modules/tag.php";
                            }else {
                                header("Location: ".$config['domain']);
                            }
                        }else if(is_numeric($params[0]) && is_numeric($params[1]) && strlen($params[0])==4 && strlen($params[1])==2) {//luu tru
                                $_SESSION['clientActiveMenu']='archives';
                                $config['create_date'] = $params[0].'-'.$params[1];
                                if(intval($params[3])>0) {
                                    $config ['page']=$params[3];
                                }
                                if (file_exists($config ['client_function_path']."/archives/archives-functions.php")) {
                                    include_once $config ['client_function_path']."/archives/archives-functions.php";
                                }
                                if (file_exists($config ['client_document_root']."/modules/archives.php")) {
                                    include_once $config ['client_document_root']."/modules/archives.php";
                                }else {
                                    header("Location: ".$config['domain']);
                                }
                            }else {
                                $config['site_title'] = 'Trang chủ - '. $config['site_title'];
                                $config['site_description'] = 'Trang chủ, '. $config['site_description'];
                                $config['site_keyword'] = 'Trang chủ, '. $config['site_keyword'];
                                if (file_exists($config ['client_function_path']."/$page/$page-functions.php")) {
                                    include_once $config ['client_function_path']."/$page/$page-functions.php";
                                }
                                if (file_exists($config ['client_document_root']."/modules/$page.php")) {
                                    include_once $config ['client_document_root']."/modules/$page.php";
                                }else {
                                    header("Location: ".$config['domain']);
                                }
                            }

}

/*
$end = microtime ();
$end = explode ( " ", $end );
$end = $end [1] + $end [0];
printf ( "<br>Page was generated by PHP %s in %f seconds", phpversion (), $end - $start );
 */
/*
$mem_usage = memory_get_usage(true);
        if ($mem_usage < 1024)
            echo $mem_usage." bytes";
        elseif ($mem_usage < 1048576)
            echo round($mem_usage/1024,2)." kilobytes";
        else
            echo round($mem_usage/1048576,2)." megabytes";

        echo "<br/>";
 */
//ob_end_flush();
print_gzipped_page();
?>